Move the randomMAC method from xm.create into server.netif. This way, it can
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Mon, 7 Nov 2005 11:36:53 +0000 (12:36 +0100)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Mon, 7 Nov 2005 11:36:53 +0000 (12:36 +0100)
be shared with other command line tools, and the xm network-attach command.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/python/xen/xend/server/netif.py
tools/python/xen/xm/create.py

index d6f7671a6e9865887bc295fb1b5507d0108b9377..782ccf0215022fe03dfbe4ea2d4f9e02a422105e 100644 (file)
@@ -21,6 +21,7 @@
 """
 
 import os
+import random
 
 from xen.xend import sxp
 from xen.xend import XendRoot
@@ -31,6 +32,25 @@ from xen.xend.server.DevController import DevController
 xroot = XendRoot.instance()
 
 
+def randomMAC():
+    """Generate a random MAC address.
+
+    Uses OUI (Organizationally Unique Identifier) AA:00:00, an
+    unassigned one that used to belong to DEC. The OUI list is
+    available at 'standards.ieee.org'.
+
+    The remaining 3 fields are random, with the first bit of the first
+    random field set 0.
+
+    @return: MAC address string
+    """
+    mac = [ 0xaa, 0x00, 0x00,
+            random.randint(0x00, 0x7f),
+            random.randint(0x00, 0xff),
+            random.randint(0x00, 0xff) ]
+    return ':'.join(map(lambda x: "%02x" % x, mac))
+
+
 class NetifController(DevController):
     """Network interface controller. Handles all network devices for a domain.
     """
@@ -57,6 +77,9 @@ class NetifController(DevController):
 
         devid = self.allocateDeviceID()
 
+        if not mac:
+            mac = randomMAC()
+
         back = { 'script' : script,
                  'mac'    : mac,
                  'handle' : "%i" % devid }
index c73d66c2f933f94766a55a36d666a741dfc49686..2a3d0929a6d8e8dc8b926654f41a313497afbca0 100644 (file)
@@ -19,7 +19,6 @@
 
 """Domain creation.
 """
-import random
 import os
 import os.path
 import string
@@ -497,24 +496,6 @@ def configure_tpmif(config_devs, vals):
             config_devs.append(['device', config_tpmif])
 
 
-def randomMAC():
-    """Generate a random MAC address.
-
-    Uses OUI (Organizationally Unique Identifier) AA:00:00, an
-    unassigned one that used to belong to DEC. The OUI list is
-    available at 'standards.ieee.org'.
-
-    The remaining 3 fields are random, with the first bit of the first
-    random field set 0.
-
-    @return: MAC address string
-    """
-    mac = [ 0xaa, 0x00, 0x00,
-            random.randint(0x00, 0x7f),
-            random.randint(0x00, 0xff),
-            random.randint(0x00, 0xff) ]
-    return ':'.join(map(lambda x: "%02x" % x, mac))
-
 def configure_vifs(config_devs, vals):
     """Create the config for virtual network interfaces.
     """
@@ -525,8 +506,6 @@ def configure_vifs(config_devs, vals):
         if idx < len(vifs):
             d = vifs[idx]
             mac = d.get('mac')
-            if not mac:
-                mac = randomMAC()
             be_mac = d.get('be_mac')
             bridge = d.get('bridge')
             script = d.get('script')
@@ -534,8 +513,7 @@ def configure_vifs(config_devs, vals):
             ip = d.get('ip')
             vifname = d.get('vifname')
         else:
-            
-            mac = randomMAC()
+            mac = None
             be_mac = None
             bridge = None
             script = None
@@ -543,7 +521,8 @@ def configure_vifs(config_devs, vals):
             ip = None
             vifname = None
         config_vif = ['vif']
-        config_vif.append(['mac', mac])
+        if mac:
+            config_vif.append(['mac', mac])
         if vifname:
             config_vif.append(['vifname', vifname])
         if be_mac:
@@ -925,8 +904,6 @@ def parseCommandLine(argv):
 
 
 def main(argv):
-    random.seed()
-
     (opts, config) = parseCommandLine(argv)
 
     if not opts: